package vn.edu.saigontech.ncms.service.impl;

import java.util.Collection;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import vn.edu.saigontech.ncms.model.system.Room;
import vn.edu.saigontech.ncms.service.OfferingService;
import vn.edu.saigontech.ncms.service.RoomService;

@SuppressWarnings("unchecked")
public class RoomServiceImpl extends HibernateGenericService<Room, Long> implements RoomService {
	
	@Override
	public Collection<Room> findAll() {
		Criteria criteria = this.createCriteria();
		criteria.addOrder(Order.asc("id"));
		return criteria.list();
	}
	
	@Override
	public Room findByCode(String code) {
		Criteria criteria = this.createCriteria();
		criteria.add(Restrictions.eq("code", code));
		return (Room)criteria.uniqueResult();
	}

	@Override
	public Collection<Room> findAllAreActive() {
		Criteria criteria = this.createCriteria();
		criteria.add(Restrictions.eq("active", true));
		criteria.addOrder(Order.asc("id"));
		return criteria.list();
	}
	
	@Override
	public boolean deleteById(Long id) {
		if(OfferingService.INSTANCE.countByRoomId(id) > 0) {
			return false;
		}
		return super.deleteById(id);
	}
}